package com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.dao.ExportProceedsDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.dao.ExportProceedsFileDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.dao.ExportProceedsSimpleDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.entity.ExportProceeds;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.entity.ExportProceedsFile;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.entity.ExportProceedsSimple;
import org.apache.commons.lang.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lianhong
 * @since 2021/11/19
 */
@Service
public class ExportProceedsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExportProceedsService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private ExportProceedsDao exportProceedsDao;
    @Resource
    private ExportProceedsSimpleDao exportProceedsSimpleDao;
    @Resource
    private ExportProceedsFileDao exportProceedsFileDao;

    public String save(String exportProceeds) {
        if (StringUtils.isEmpty(exportProceeds)) return StringUtils.EMPTY;
        String text = SecurityUtil.decrypt3DES(token, exportProceeds);
        try {
            JSONObject req = JSON.parseObject(text);
            if (req == null) return StringUtils.EMPTY;

            String entryId = req.getString("entryId");
            String socialCreditCode = req.getString("socialCreditCode");
            JSONObject proceedsObj = req.getJSONObject("exportProceeds");
            ExportProceeds proceeds = new ExportProceeds(entryId, socialCreditCode, proceedsObj);

            ExportProceedsSimple simple = proceeds.buildExportProceedsSimple();
            if (simple == null) return StringUtils.EMPTY;

            this.remove(proceeds.getEntryId());

            exportProceedsDao.save(proceeds);
            exportProceedsSimpleDao.save(simple);
            return SecurityUtil.encrypt3DES(token, proceeds.getExportProceeds());
        } catch (Exception e) {
            LOGGER.error("保存出口收汇错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String details(String entryId) {
        ExportProceeds proceeds = exportProceedsDao.findByEntryId(entryId);
        if (proceeds == null) return StringUtils.EMPTY;
        return SecurityUtil.encrypt3DES(token, proceeds.getExportProceeds());
    }

    public void remove(String entryId) {
        exportProceedsDao.delete(entryId);
        exportProceedsSimpleDao.delete(entryId);
    }

    public boolean exists(String entryId) {
        return exportProceedsSimpleDao.exists(entryId);
    }

    public String exportProceedsFile(String entryId) {
        return exportProceedsFileDao.findByEntryId(entryId);
    }

    public void saveExportProceedsFile(String entryId, String storedFile) {
        ExportProceedsFile exportProceedsFile = new ExportProceedsFile(entryId, storedFile);
        exportProceedsFileDao.save(exportProceedsFile);
    }

    public boolean exportProceedsFileStored(String entryId) {
        return exportProceedsFileDao.exists(entryId);
    }

    public String existsExportProceeds(List<String> entryIds) {
        if (CollectionUtils.isEmpty(entryIds)) return StringUtils.EMPTY;
        List<List<String>> parts = Lists.partition(entryIds, 500);
        List<String> result = new ArrayList<>();
        parts.forEach(item -> result.addAll(exportProceedsSimpleDao.exists(item)));
        return StringUtils.join(result, ",");
    }
}
